#include "Sphere.h"

#include "../Maths/Math.h"
#include "../Components/Renderer/MeshFilter.h"
#include "../Components/Renderer/MeshRenderer.h"

using namespace std;

Sphere::Sphere()
{
	_name = "Sphere";
	InitVertex();	
}

Sphere::Sphere(string name)
{
	_name = name;
	InitVertex();
}

Sphere::~Sphere()
{

}

void Sphere::InitVertex()
{
	//Vertex
	vector<float> vertexVector;
	float step = 0.1f;
	int nbLine = 0;
	int nbVertexByLine = 0;
	int size = 1;

	//float i = -Math::PI / 2.0f;
	for(float i = -Math::PI / 2.0f; i < Math::PI / 2.0f; i += step)
	{
		nbVertexByLine = 0;

		for(float j = - Math::PI; j < Math::PI ; j+=step)
		{
			vertexVector.push_back(size * cos(i) * cos(j));
			vertexVector.push_back(size * cos(i) * sin(j));
			vertexVector.push_back(size * sin(i));
			nbVertexByLine ++;
		}
		nbLine++;
	}

	vector<unsigned int>indexVector;
	for(int i = 0; i < nbLine - 1; ++i)
	{
		for(int j = 0; j < nbVertexByLine ; ++j)
		{
			//int size = vertexVector.size();
			//if(j + 1 + nbVertexByLine * (i + 1) > size || j + 1 + nbVertexByLine * i > size || j + nbVertexByLine * i > size || j + nbVertexByLine * i > size || j + 2 + nbVertexByLine * (i + 1) > size || j + nbVertexByLine * (i + 1) > size)
				//continue;
			
			if(j + 1 >= nbVertexByLine)
				continue;
			
			//Faces 1
			indexVector.push_back(j + nbVertexByLine * i);
			indexVector.push_back(j + 1 + nbVertexByLine * i);
			indexVector.push_back(j + 1 + nbVertexByLine * (i + 1));
			

			if(j + 2 >= nbVertexByLine)
				continue;
			
			//Faces 2
			indexVector.push_back(j + nbVertexByLine * i);
			indexVector.push_back(j + nbVertexByLine * (i + 1));
			indexVector.push_back(j + 2 + nbVertexByLine * (i + 1));
			
		}
	}

	//Index Vertex


	//Size Array
	unsigned int const sizeVertex = vertexVector.size();//(8 * 3);
	unsigned int const sizeIndex = indexVector.size();
	unsigned int const sizeColor = sizeVertex;//8*3;//sizeVertex / 3;

	//_nbVertex = sizeVertex / 3;
	//_nbIndex = sizeIndex;

	//Allocation
	float* vertex = new float[sizeVertex];
	short* index = new short[sizeIndex];


	//Vertex copy
	for(unsigned int i(0); i < sizeVertex; ++i)
		vertex[i] = vertexVector[i];


	//Index copy
	for(unsigned int i(0); i < sizeIndex; ++i)
		index[i] = indexVector[i];

	unsigned int indexCount = sizeIndex;
	unsigned int vertexCount = sizeVertex / 3;


	AddComponent(new MeshFilter());
	MeshFilter* mesh = GetComponent<MeshFilter>();
	mesh->SetIndex(index, indexCount);
	//mesh->SetNormals(complex_cube_normales);
	mesh->SetVertex(vertex, vertexCount);
	mesh->Apply();

	AddComponent(new MeshRenderer());

}

/*
void Sphere::draw(Matrice &modelview, Matrice &projection)
{
	modelview.push();

	modelview.translate(_position[0],_position[1],_position[2]);

	//Shader & Vertex Init
	glUseProgram(m_shaderColor.getProgramID());
	glEnableVertexAttribArray(0);
	glEnableVertexAttribArray(1);

	//Shader Values
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, m_vertex);
	glUniformMatrix4fv(glGetUniformLocation(m_shaderColor.getProgramID(), "projection"), 1, GL_TRUE, projection.getValues());
	glUniformMatrix4fv(glGetUniformLocation(m_shaderColor.getProgramID(), "modelview"), 1, GL_TRUE, modelview.getValues());

	
	//Face 1
	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE,0, _color);
	glDrawElements(GL_TRIANGLES, _nbIndex, GL_UNSIGNED_INT, m_index);
	
	/*
	//Draw Vertex
	glBegin(GL_POINTS);
	
	for(int i = 0; i < _nbVertex; i++){
		glVertex3f(m_vertex[(i * 3)], m_vertex[(i * 3)+1], m_vertex[(i * 3)+2]);
	}
	glEnd();
	*/
/*
	//Disable shader and vertex		
	glDisableVertexAttribArray(1);
	glDisableVertexAttribArray(0);
	glUseProgram(0);

	modelview.pop();
}
*/
